package com.young.batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 批量发送消息
 * 批量发送消息可以显著提高传递传递小信息的性能，限制是这些消息应该有相同的topic,相同的waitStoreMsgOK，而且不能是延时消息
 * 此外，这一批消息的总大小不能超过4M,如果超过4M需要进行消息的分割，可以查看 ListSplitter
 */
public class BatchProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("batch_producer_group");
        producer.setNamesrvAddr("192.168.188.101:9876;192.168.188.102:9876;");
        producer.start();
        List<Message> messageList = new ArrayList<Message>();
        messageList.add(new Message("batch_topic", "batch_message_1".getBytes(StandardCharsets.UTF_8)));
        messageList.add(new Message("batch_topic", "batch_message_2".getBytes(StandardCharsets.UTF_8)));
        messageList.add(new Message("batch_topic", "batch_message_3".getBytes(StandardCharsets.UTF_8)));
        producer.send(messageList);
        producer.shutdown();
    }
}
